package com.oschat.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.oschat.entity.enums.PrettyAccountStatusEnum;
import com.oschat.entity.enums.ResponseCodeEnum;
import com.oschat.entity.po.UserInfo;
import com.oschat.entity.query.UserInfoQuery;
import com.oschat.exception.BusinessException;
import com.oschat.mappers.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.oschat.entity.enums.PageSize;
import com.oschat.entity.query.UserInfoPrettyQuery;
import com.oschat.entity.po.UserInfoPretty;
import com.oschat.entity.vo.PaginationResultVO;
import com.oschat.entity.query.SimplePage;
import com.oschat.mappers.UserInfoPrettyMapper;
import com.oschat.service.UserInfoPrettyService;
import com.oschat.utils.StringTools;


/**
 * 靓号表 业务接口实现
 */
@Service("userInfoPrettyService")
public class UserInfoPrettyServiceImpl implements UserInfoPrettyService {

    @Resource
    private UserInfoPrettyMapper<UserInfoPretty, UserInfoPrettyQuery> userInfoPrettyMapper;

    @Resource
    private UserInfoMapper<UserInfo, UserInfoQuery> userInfoMapper;

    /**
     * 根据条件查询列表
     */
    @Override
    public List<UserInfoPretty> findListByParam(UserInfoPrettyQuery param) {
        return this.userInfoPrettyMapper.selectList(param);
    }

    /**
     * 根据条件查询列表
     */
    @Override
    public Integer findCountByParam(UserInfoPrettyQuery param) {
        return this.userInfoPrettyMapper.selectCount(param);
    }

    /**
     * 分页查询方法
     */
    @Override
    public PaginationResultVO<UserInfoPretty> findListByPage(UserInfoPrettyQuery param) {
        int count = this.findCountByParam(param);
        int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

        SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
        param.setSimplePage(page);
        List<UserInfoPretty> list = this.findListByParam(param);
        PaginationResultVO<UserInfoPretty> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
        return result;
    }

    /**
     * 新增
     */
    @Override
    public Integer add(UserInfoPretty bean) {
        return this.userInfoPrettyMapper.insert(bean);
    }

    /**
     * 批量新增
     */
    @Override
    public Integer addBatch(List<UserInfoPretty> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.userInfoPrettyMapper.insertBatch(listBean);
    }

    /**
     * 批量新增或者修改
     */
    @Override
    public Integer addOrUpdateBatch(List<UserInfoPretty> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.userInfoPrettyMapper.insertOrUpdateBatch(listBean);
    }

    /**
     * 多条件更新
     */
    @Override
    public Integer updateByParam(UserInfoPretty bean, UserInfoPrettyQuery param) {
        StringTools.checkParam(param);
        return this.userInfoPrettyMapper.updateByParam(bean, param);
    }

    /**
     * 多条件删除
     */
    @Override
    public Integer deleteByParam(UserInfoPrettyQuery param) {
        StringTools.checkParam(param);
        return this.userInfoPrettyMapper.deleteByParam(param);
    }

    /**
     * 根据Id获取对象
     */
    @Override
    public UserInfoPretty getUserInfoPrettyById(Integer id) {
        return this.userInfoPrettyMapper.selectById(id);
    }

    /**
     * 根据Id修改
     */
    @Override
    public Integer updateUserInfoPrettyById(UserInfoPretty bean, Integer id) {
        return this.userInfoPrettyMapper.updateById(bean, id);
    }

    /**
     * 根据Id删除
     */
    @Override
    public Integer deleteUserInfoPrettyById(Integer id) {
        return this.userInfoPrettyMapper.deleteById(id);
    }

    /**
     * 根据UserId获取对象
     */
    @Override
    public UserInfoPretty getUserInfoPrettyByUserId(String userId) {
        return this.userInfoPrettyMapper.selectByUserId(userId);
    }

    /**
     * 根据UserId修改
     */
    @Override
    public Integer updateUserInfoPrettyByUserId(UserInfoPretty bean, String userId) {
        return this.userInfoPrettyMapper.updateByUserId(bean, userId);
    }

    /**
     * 根据UserId删除
     */
    @Override
    public Integer deleteUserInfoPrettyByUserId(String userId) {
        return this.userInfoPrettyMapper.deleteByUserId(userId);
    }

    /**
     * 根据Email获取对象
     */
    @Override
    public UserInfoPretty getUserInfoPrettyByEmail(String email) {
        return this.userInfoPrettyMapper.selectByEmail(email);
    }

    /**
     * 根据Email修改
     */
    @Override
    public Integer updateUserInfoPrettyByEmail(UserInfoPretty bean, String email) {
        return this.userInfoPrettyMapper.updateByEmail(bean, email);
    }

    /**
     * 根据Email删除
     */
    @Override
    public Integer deleteUserInfoPrettyByEmail(String email) {
        return this.userInfoPrettyMapper.deleteByEmail(email);
    }

    @Override
    public void saveAccount(UserInfoPretty userInfoPretty) {
        if (userInfoPretty.getId() != null) {
            UserInfoPretty dbInfo = this.userInfoPrettyMapper.selectById(userInfoPretty.getId());
            if (PrettyAccountStatusEnum.USEED.getStatus().equals(dbInfo.getStatus())) {
                throw new BusinessException(ResponseCodeEnum.CODE_600);
            }
        }
        UserInfoPretty dbInfo = this.userInfoPrettyMapper.selectByEmail(userInfoPretty.getEmail());

        //新增的时候判断邮箱是否存在
        if (userInfoPretty.getId() == null && dbInfo != null) {
            throw new BusinessException("靓号邮箱已经存在");
        }

        //修改时判断邮箱是否存在
        if (userInfoPretty.getId() != null && dbInfo != null && dbInfo.getId() != null && !userInfoPretty.getId().equals(dbInfo.getId())) {
            throw new BusinessException("靓号邮箱已经存在");
        }

        //判断靓号是否存在
        dbInfo = this.userInfoPrettyMapper.selectByUserId(userInfoPretty.getUserId());
        if (userInfoPretty.getId() == null && dbInfo != null) {
            throw new BusinessException("靓号已经存在");
        }

        //修改时判断邮箱是否存在
        if (userInfoPretty.getId() != null && dbInfo != null && dbInfo.getId() != null && !userInfoPretty.getId().equals(dbInfo.getId())) {
            throw new BusinessException("靓号已经存在");
        }

        //判断邮箱是否已经注册
        UserInfo userInfo = this.userInfoMapper.selectByEmail(userInfoPretty.getEmail());
        if (userInfo != null) {
            throw new BusinessException("靓号邮箱已经被注册");
        }
        userInfo = this.userInfoMapper.selectByUserId(userInfoPretty.getUserId());
        if (userInfo != null) {
            throw new BusinessException("靓号已经被注册");
        }
        if (userInfoPretty.getId() != null) {
            this.userInfoPrettyMapper.updateById(userInfoPretty, userInfoPretty.getId());
        } else {
            userInfoPretty.setStatus(PrettyAccountStatusEnum.NO_USE.getStatus());
            this.userInfoPrettyMapper.insert(userInfoPretty);
        }
    }
}